package com.cqan.log.controller;

import static com.cqan.common.ViewName.*;

import java.util.List;
import java.util.StringTokenizer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cqan.controller.BaseController;
import com.cqan.exception.AppException;
import com.cqan.log.entity.DigitalAccessLog;
import com.cqan.log.service.DigitalAccessLogService;
import com.cqan.orm.PropertyFilter;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("/log/digital")
public class DigitalAccessLogController extends BaseController<DigitalAccessLog> {

	@Resource(name="digitalAccessLogService")
	private DigitalAccessLogService digitalAccessLogService;
	/**
	 * 权限管理列表
	 * @param pageNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.htm")
	public ModelAndView list(Integer pageNo,HttpServletRequest request)throws AppException{
		ModelAndView mav = new ModelAndView(DIGITAL_ACCESS_LOG_LIST);
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		page.setPageNo(pageNo);
		page.setOrderBy("ctime");
		page.setOrder("DESC");
		page = digitalAccessLogService.search(page, filters);
		mav.addObject("page", page);
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value="/delete.htm",method=RequestMethod.POST)
	public String delete(String ids)throws AppException{
		try {
			if (ids!=null) {
				StringTokenizer st = new StringTokenizer(ids,"-");
				List<Long> list = Lists.newArrayList();
				while (st.hasMoreTokens()) {
					String id = st.nextToken();
					if (id!=null&&!"".equals(id)) {
						list.add(Long.parseLong(id));
					}
				}
				digitalAccessLogService.delete(list);
				return "删除成功！";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "该日志暂不能删除！";
		}
		return  "删除失败！";
	}
	
}
